import util from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectList: [],
    isPage: false,
    isNearby: true,
    isShow:true,
    id:0,
    name:'',
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this._getAddress()
    wx.showLoading({
      title: '加载中'
    })
  },
  //获取用户地理位置
  _getAddress() {
    new Promise((resolve, reject) => {
        wx.getSetting({
          success(res) {
            resolve(res)
          }
        })
      })
      .then(res => {
        //没有授权进行授权
        if (!res.authSetting['scope.userLocation']) {
          return new Promise((resolve, reject) => {
            wx.authorize({
              scope: 'scope.userLocation',
              success(res) {
                resolve(res)
              }
            })
          })
        } else {
          var _this = this
          wx.getLocation({
            type: 'gcj02',
            success(res) {
              console.log(`纬度${res.latitude}，经度${res.longitude},水平精度${res.horizontalAccuracy}`)
              _this._getPosition(res.latitude, res.longitude)
            }
          })
        }
      })
      .then(res => {
        if (res) {
          // 授权后执行回调
          return new Promise((resolve, reject) => {
            wx.getLocation({
              type: 'gcj02',
              success(res) {
                resolve(res)
              }
            })
          })
        }
      })
      .then(res => {
        if (res) {
          console.log(`经度${res.longitude},纬度${res.latitude}，水平精度${res.horizontalAccuracy}`)
          this._getPosition(res.latitude, res.longitude)
        }
      }, (error) => {
        console.log(error)
      })
  },
  //获取位置
  _getPosition(Y, X) {
    const url = util.api.nearby
    const method = 'GET'
    const data = {
      lat: Y,
      lng: X,
      radius: 20000
    }
    util.sendRequest(url, method, data)
      .then(res => {
        console.log(res)
        const selectList = res.data.list
        //没有获取到油站
        if (selectList.length == 0) {
          this.setData({
            isNearby: false
          })
        } else {
          //如果只有一个油站小于1000米，直接跳转
          let num = 0
          selectList.forEach(item => {
            if (!item.distance.includes('KM')) {
              num++
            }
          })
          console.log(num)
          if (num == 1) { //等于1跳转
            const id = selectList[0].merchant_id
            const name = selectList[0].merchant_name
            this.runPay(id, name)
          } else { //多个渲染页面
            this.setData({
              isPage: true,
              selectList
            })
          }
          console.log(selectList)
          wx.hideLoading()
        }
      })

  },
  //去位置
  goPosition(e) {
    const index = e.currentTarget.dataset.index
    const latitude = this.data.selectList[index].lat
    const longitude = this.data.selectList[index].lng
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 18,
    })
  },
  //去付款
  goPay(e) {
    const id = e.currentTarget.dataset.id
    const name = e.currentTarget.dataset.name
    const distance = e.currentTarget.dataset.distance
    this.setData({
      isShow:false,
      id,name,distance
    })

  },
  runPay() {
    wx.navigateTo({
      url: `../gas/gas?merchant_id=${this.data.id}&merchant_name=${this.data.name}`
    })
    this.closeAgain()
  },
  //关闭
  closeAgain(){
    this.setData({
      isShow:true
    })
  },
  // 分享
  onShareAppMessage() {

  }

})